package com.oocl.admin_platform.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

public class DBUtil {

	private static DataSource ds = null;
	
//	private static String CLASSNAME = "oracle.jdbc.OracleDriver";
//	private static String URL = "jdbc:oracle:thin:@10.222.29.158:1521:xe";
//	private static String NAME = "harry";
//	private static String PASSWORD="123";
//	private static BasicDataSource dataSource;
//	static{
//		dataSource=new BasicDataSource();
//		dataSource.setUrl(URL);
//		dataSource.setDriverClassName(CLASSNAME);
//		dataSource.setUsername(NAME);
//		dataSource.setPassword(PASSWORD);
//		dataSource.setMaxActive(100);
//	}
	
//	public static Connection createConnectionWithCommon(){
//		Connection connection=null;
//		try {
//			connection=dataSource.getConnection();
//			System.out.println(connection);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return connection;
//	}
	
	

	public static DataSource getDataSource() throws Exception {
		if (ds == null) {
			InitialContext initContext = new InitialContext();
			ds = (DataSource) initContext
					.lookup("java:/comp/env/jdbc/myoracle");
		}
		return ds;
	}

	public static Connection createConnectionWithTomcat() {
		Connection connection=null;
			try {
				connection = getDataSource().getConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return connection;
	}

	
	
	
	
	public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
		try {
			if (resultSet!=null) {
				resultSet.close();
			}
			if (preparedStatement!=null) {
				preparedStatement.close();
			}
			if (connection!=null) {
				connection.close();
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
